package com.smartwebx.ha.biz.assign.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.assign.HaStationStationGroupManager;
import com.smartwebx.ha.dal.assign.bean.HaStationArea;
import com.smartwebx.ha.dal.assign.bean.HaStationAreaExample;
import com.smartwebx.ha.dal.assign.bean.HaStationStationGroup;
import com.smartwebx.ha.dal.assign.bean.HaStationStationGroupExample;
import com.smartwebx.ha.dal.assign.bean.HaStationStationGroupExample.Criteria;
import com.smartwebx.ha.dal.assign.dao.HaStationAreaMapper;
import com.smartwebx.ha.dal.assign.dao.HaStationStationGroupMapper;
import com.smartwebx.ha.dal.util.MyFunctions;

@Service
public class HaStationStationGroupManagerImpl implements
		HaStationStationGroupManager {
	@Autowired
	private HaStationStationGroupMapper haStationStationGroupDao;
	@Autowired
	private HaStationAreaMapper haStationAreaDao;

	@Override
	public void updateStationStationGroup(String operate, int areaId,
			List<Integer> stations, final int stationGroupId) {
		if (stations.isEmpty()) {
			return;
		} else if (Objects.equal(operate, "rm")) {
			HaStationStationGroupExample haStationStationGroupExample = new HaStationStationGroupExample();
			Criteria criteria = haStationStationGroupExample.createCriteria();
			criteria.andStationGroupIdEqualTo(stationGroupId).andStatIdIn(
					stations);
			haStationStationGroupDao
					.deleteByExample(haStationStationGroupExample);
		} else if (Objects.equal(operate, "add")) {
			HaStationAreaExample haStationAreaExample = new HaStationAreaExample();
			haStationAreaExample.createCriteria().andAreaIdEqualTo(areaId);
			List<HaStationArea> haStationAreasByAreaId = haStationAreaDao
					.selectByExample(haStationAreaExample);
			Collection<Integer> idsByAreaId = Collections2.transform(
					haStationAreasByAreaId, MyFunctions.getIntIdFuns());
			ImmutableList<Integer> checkedStations = FluentIterable
					.from(stations).filter(Predicates.in(idsByAreaId)).toList();

			List<HaStationStationGroup> result = Lists.transform(
					checkedStations,
					new Function<Integer, HaStationStationGroup>() {
						public HaStationStationGroup apply(Integer input) {
							return new HaStationStationGroup(input,
									stationGroupId);
						}
					});
			haStationStationGroupDao.insertBatch(result);
		}
	}

	@Override
	public List<Integer> getStatIdByStatGroupId(int statGroupId) {
		HaStationStationGroupExample haStationStationGroupExample = new HaStationStationGroupExample();
		Criteria criteria = haStationStationGroupExample.createCriteria();
		criteria.andStationGroupIdEqualTo(statGroupId);
		List<HaStationStationGroup> result = haStationStationGroupDao
				.selectByExample(haStationStationGroupExample);
		List<Integer> statIds = Lists.newArrayList();
		for (HaStationStationGroup haStationStationGroup : result) {
			statIds.add(haStationStationGroup.getStatId());
		}
		return statIds;
	}
}